import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
import os

img_read = 'text.jpg'
img_output = 'opening_img.jpg'
'''
处理图片路径变量 : img_read
图片输出路径变量 : img_output
'''



img_ori = cv2.imread(img_read)
gray = cv2.cvtColor(img_ori, cv2.COLOR_BGRA2GRAY)
m=[180,200]
for i in range(len(m)):
    _, binary_img = cv2.threshold(gray, m[i], 255, cv2.THRESH_BINARY)
    kernel1 = np.ones((1,1), np.uint8)
    dilated_img = cv2.dilate(binary_img, kernel1, iterations=1)
    kernel2 = np.ones((1,1), np.uint8)
    eroded_img = cv2.erode(dilated_img, kernel2, iterations=1)
    kernel3 = np.ones((1, 1), np.uint8)
    opening_img = cv2.morphologyEx(eroded_img, cv2.MORPH_OPEN, kernel3)
    drawing = False
    ix, iy = -1, -1
    img = np.zeros((512, 512, 3), dtype=np.uint8)
    cur_img=cv2.cvtColor(opening_img,cv2.COLOR_GRAY2BGR)
    contours, _ = cv2.findContours(opening_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    gaosi = cv2.GaussianBlur(cur_img,(5,5),0)
    if i==0:
        img1=cur_img
    else:
        img2=cur_img


img_end = cv2.bitwise_or(img1,img2)
cv2.imshow('text',img_end)
cv2.waitKey(0)
cv2.destroyAllWindows()


cv2.imwrite(img_output,img_end)